package com.hz.friends.chat;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.content.Context;
import android.text.Spannable;
import android.text.Spannable.Factory;
import android.text.style.ImageSpan;

import com.hz.friends.activity.R;


public class SmileUtils {
	public static final String ee_001 = "[*001*]";
	public static final String ee_002 = "[*002*]";
	public static final String ee_003 = "[*003*]";
	public static final String ee_004 = "[*004*]";
	public static final String ee_005 = "[*005*]";
	public static final String ee_006 = "[*006*]";
	public static final String ee_007 = "[*007*]";
	public static final String ee_008 = "[*008*]";
	public static final String ee_009 = "[*009*]";
	public static final String ee_010 = "[*010*]";
	public static final String ee_011 = "[*012*]";
	public static final String ee_012 = "[*013*]";
	public static final String ee_013 = "[*014*]";
	public static final String ee_014 = "[*015*]";
	public static final String ee_015 = "[*016*]";
	public static final String ee_016 = "[*017*]";
	public static final String ee_017 = "[*018*]";
	public static final String ee_018 = "[*019*]";
	public static final String ee_019 = "[*020*]";
	public static final String ee_020 = "[*021*]";
	public static final String ee_021 = "[*022*]";
	public static final String ee_022 = "[*023*]";
	public static final String ee_023 = "[*024*]";
	public static final String ee_024 = "[*025*]";
	public static final String ee_025 = "[*026*]";
	public static final String ee_026 = "[*027*]";
	public static final String ee_027 = "[*028*]";
	public static final String ee_028 = "[*029*]";
	public static final String ee_029 = "[*030*]";
	public static final String ee_030 = "[*031*]";
	public static final String ee_031 = "[*032*]";
	public static final String ee_032 = "[*033*]";
	public static final String ee_033 = "[*034*]";
	public static final String ee_034 = "[*035*]";
	public static final String ee_035 = "[*036*]";
	public static final String ee_036 = "[*037*]";
	public static final String ee_037 = "[*038*]";
	public static final String ee_038 = "[*039*]";
	public static final String ee_039 = "[*040*]";
	public static final String ee_040 = "[*041*]";
	public static final String ee_041 = "[*042*]";
	public static final String ee_042 = "[*043*]";
	public static final String ee_043 = "[*044*]";
	public static final String ee_044 = "[*045*]";
	public static final String ee_045 = "[*046*]";
	public static final String ee_046 = "[*047*]";
	public static final String ee_047 = "[*048*]";
	public static final String ee_048 = "[*049*]";
	public static final String ee_049 = "[*050*]";
	public static final String ee_050 = "[*051*]";
	public static final String ee_051 = "[*052*]";
	public static final String ee_052 = "[*053*]";
	public static final String ee_053 = "[*054*]";
	public static final String ee_054 = "[*055*]";
	public static final String ee_055 = "[*056*]";
	public static final String ee_056 = "[*057*]";
	
	private static final Factory spannableFactory = Spannable.Factory
	        .getInstance();
	
	private static final Map<Pattern, Integer> emoticons = new HashMap<Pattern, Integer>();

	static {
		
	    addPattern(emoticons, ee_001, R.drawable.ee_001);
	    addPattern(emoticons, ee_002, R.drawable.ee_002);
	    addPattern(emoticons, ee_003, R.drawable.ee_003);
	    addPattern(emoticons, ee_004, R.drawable.ee_004);
	    addPattern(emoticons, ee_005, R.drawable.ee_005);
	    addPattern(emoticons, ee_006, R.drawable.ee_006);
	    addPattern(emoticons, ee_007, R.drawable.ee_007);
	    addPattern(emoticons, ee_008, R.drawable.ee_008);
	    addPattern(emoticons, ee_009, R.drawable.ee_009);
	    addPattern(emoticons, ee_010, R.drawable.ee_010);
	    addPattern(emoticons, ee_011, R.drawable.ee_011);
	    addPattern(emoticons, ee_012, R.drawable.ee_012);
	    addPattern(emoticons, ee_013, R.drawable.ee_013);
	    addPattern(emoticons, ee_014, R.drawable.ee_014);
	    addPattern(emoticons, ee_015, R.drawable.ee_015);
	    addPattern(emoticons, ee_016, R.drawable.ee_016);
	    addPattern(emoticons, ee_017, R.drawable.ee_017);
	    addPattern(emoticons, ee_018, R.drawable.ee_018);
	    addPattern(emoticons, ee_019, R.drawable.ee_019);
	    addPattern(emoticons, ee_020, R.drawable.ee_020);
	    addPattern(emoticons, ee_021, R.drawable.ee_021);
	    addPattern(emoticons, ee_022, R.drawable.ee_022);
	    addPattern(emoticons, ee_023, R.drawable.ee_023);
	    addPattern(emoticons, ee_024, R.drawable.ee_024);
	    addPattern(emoticons, ee_025, R.drawable.ee_025);
	    addPattern(emoticons, ee_026, R.drawable.ee_026);
	    addPattern(emoticons, ee_027, R.drawable.ee_027);
	    addPattern(emoticons, ee_028, R.drawable.ee_028);
	    addPattern(emoticons, ee_029, R.drawable.ee_029);
	    addPattern(emoticons, ee_030, R.drawable.ee_030);
	    addPattern(emoticons, ee_031, R.drawable.ee_031);
	    addPattern(emoticons, ee_032, R.drawable.ee_032);
	    addPattern(emoticons, ee_033, R.drawable.ee_033);
	    addPattern(emoticons, ee_034, R.drawable.ee_034);
	    addPattern(emoticons, ee_035, R.drawable.ee_035);
	    addPattern(emoticons, ee_036, R.drawable.ee_036);
	    addPattern(emoticons, ee_037, R.drawable.ee_037);
	    addPattern(emoticons, ee_038, R.drawable.ee_038);
	    addPattern(emoticons, ee_039, R.drawable.ee_039);
	    addPattern(emoticons, ee_040, R.drawable.ee_040);
	    addPattern(emoticons, ee_041, R.drawable.ee_041);
	    addPattern(emoticons, ee_042, R.drawable.ee_042);
	    addPattern(emoticons, ee_043, R.drawable.ee_043);
	    addPattern(emoticons, ee_044, R.drawable.ee_044);
	    addPattern(emoticons, ee_045, R.drawable.ee_045);
	    addPattern(emoticons, ee_046, R.drawable.ee_046);
	    addPattern(emoticons, ee_047, R.drawable.ee_047);
	    addPattern(emoticons, ee_048, R.drawable.ee_048);
	    addPattern(emoticons, ee_049, R.drawable.ee_049);
	    addPattern(emoticons, ee_050, R.drawable.ee_050);
	    addPattern(emoticons, ee_051, R.drawable.ee_051);
	    addPattern(emoticons, ee_052, R.drawable.ee_052);
	    addPattern(emoticons, ee_053, R.drawable.ee_053);
	    addPattern(emoticons, ee_054, R.drawable.ee_054);
	    addPattern(emoticons, ee_055, R.drawable.ee_055);
	    addPattern(emoticons, ee_056, R.drawable.ee_056);
	}

	private static void addPattern(Map<Pattern, Integer> map, String smile,
	        int resource) {
	    map.put(Pattern.compile(Pattern.quote(smile)), resource);
	}

	/**
	 * replace existing spannable with smiles
	 * @param context
	 * @param spannable
	 * @return
	 */
	public static boolean addSmiles(Context context, Spannable spannable) {
	    boolean hasChanges = false;
	    for (Entry<Pattern, Integer> entry : emoticons.entrySet()) {
	        Matcher matcher = entry.getKey().matcher(spannable);
	        while (matcher.find()) {
	            boolean set = true;
	            for (ImageSpan span : spannable.getSpans(matcher.start(),
	                    matcher.end(), ImageSpan.class))
	                if (spannable.getSpanStart(span) >= matcher.start()
	                        && spannable.getSpanEnd(span) <= matcher.end())
	                    spannable.removeSpan(span);
	                else {
	                    set = false;
	                    break;
	                }
	            if (set) {
	                hasChanges = true;
	                spannable.setSpan(new ImageSpan(context, entry.getValue()),
	                        matcher.start(), matcher.end(),
	                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
	            }
	        }
	    }
	    return hasChanges;
	}

	public static Spannable getSmiledText(Context context, CharSequence text) {
	    Spannable spannable = spannableFactory.newSpannable(text);
	    addSmiles(context, spannable);
	    return spannable;
	}
	
	public static boolean containsKey(String key){
		boolean b = false;
		for (Entry<Pattern, Integer> entry : emoticons.entrySet()) {
	        Matcher matcher = entry.getKey().matcher(key);
	        if (matcher.find()) {
	        	b = true;
	        	break;
	        }
		}
		
		return b;
	}
	
//	public void xxx(String name){
//		for (int i = 0; i < 35; i++) {
//			if()
//		}
//	}
	
	
}
